package com.example.shop.mapper;

import com.example.shop.entity.OrderItem;
import com.example.shop.model.resp.OrderItemDetail;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author qwer3
 * @version 1.0.0
 * @create 2025/11/2 15:55
 */
@Mapper
public interface OrderItemMapper {

    @Insert("""
INSERT INTO mi_order_item (item_id, shop_id, order_id, goods_id, sku_id, nums, item_price, 
                           create_time, update_time, operator_id, is_delete) 
VALUES (#{itemId}, #{shopId}, #{orderId}, #{goodsId}, #{skuId}, #{nums}, #{itemPrice}, now(), now(), null, 0);
""")
    void createOrderItem(OrderItem orderItem);

    @Insert("""
<script>
INSERT INTO mi_order_item (item_id, shop_id, order_id, goods_id, sku_id, nums, item_price, 
                           create_time, update_time, operator_id, is_delete) 
VALUES
    <foreach collection="list" item="item" separator=",">
        (#{item.itemId}, #{item.shopId}, #{item.orderId}, #{item.goodsId}, #{item.skuId},
        #{item.nums}, #{item.itemPrice}, NOW(), NOW(), NULL, 0)
    </foreach>
</script>
""")
    void batchCreateOrderItem(List<OrderItem> orderItems);

    @Select("""
SELECT
    o.order_id,
    oi.item_id,
    oi.shop_id as shopCode,
    oi.goods_id,
    g.title as goods_name,
    oi.sku_id as skuKey,
    oi.nums as quantity,
    oi.item_price as price,
    sku.cover_image_id as skuImage,
    sku.attr as skuName
FROM mi_order o
JOIN mi_order_item oi ON o.order_id = oi.order_id
JOIN mi_goods g ON oi.goods_id = g.goods_id
JOIN mi_goods_sku sku ON oi.sku_id = sku.sku_id
WHERE o.user_id = #{userId};
""")
    List<OrderItemDetail> getOrderList(String userId);
}
